package com.zx.idc.backend.gui.api.watch.handler;

import com.zx.idc.backend.gui.api.ftp.Stat;
import com.zx.idc.backend.gui.api.watch.handler.base.AbstractFtpUploadResultHandler;
import com.zx.idc.backend.gui.api.watch.pojo.FtpUploadResult;
import com.zx.idc.cache.ins.InsLogCountCacheService;
import com.zx.idc.common.constants.InstructionType;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.ins.entity.InsMonitor;
import com.zx.idc.ds.ins.service.IInsMonitorService;
import com.zx.idc.xml.entity.common.filtermonitor.log.Log;
import com.zx.idc.xml.entity.monitorresult.MonitorResult;
import com.zx.idc.xml.utils.JaxbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.zx.idc.common.attributes.FTPReportDataTypeAttributes.FTP_ILLEGAL_INFORMATION_MONITORING_RECORD;

/**
 * 违法信息监测记录结果处理器
 */
@Component
//@Transactional(rollbackFor = Exception.class) //不需要声明事务,当前的类相当于controller
public class IllegalInformationMonitorRecordResultHandler extends AbstractFtpUploadResultHandler {

    @Autowired
    private IInsMonitorService iInsMonitorService;

    @Autowired
    private InsLogCountCacheService insLogCountCacheService;


    /**
     * 匹配
     *
     * @param type
     * @return
     */
    @Override
    public boolean adapter(String type) {
        return FTP_ILLEGAL_INFORMATION_MONITORING_RECORD.equals(type);
    }

    /**
     * 上报成功回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadSuccess(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("Illegal information monitoring record result success processor start!");
        updateReportStatus(filePath, ReportStatusConstant.REPORTED);
        log.debug("Illegal information monitoring record result success processor done!");
    }

    /**
     * 上报异常回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadFailed(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("Illegal information monitoring record result failed processor start!");
        updateReportStatus(filePath, ReportStatusConstant.REPORTING_EXCEPTION);
        log.debug("Illegal information monitoring record result failed processor done!");
    }

    /**
     * 更新上报状态模板
     *
     * @param filePath     源文件全路径
     * @param reportStatus
     */
    private void updateReportStatus(String filePath, Integer reportStatus) {
        MonitorResult monitorResult = JaxbUtils.xmlFileToBean(filePath, MonitorResult.class);
        List<Log> log = monitorResult.getLog();
        if (ListUtil.isEmpty(log)) {
            return;
        }

        //统计已上报记录
        Map<Long, List<Log>> groupMap = log.stream().collect(Collectors.groupingBy(Log::getCommandId));
        List<InsMonitor> updateCommandList = groupMap.keySet().stream().map(commandId -> {
            InsMonitor insMonitor = new InsMonitor();
            insMonitor.setId(commandId+"");
            insMonitor.setReportStatus(reportStatus);
            return insMonitor;
        }).collect(Collectors.toList());

        iInsMonitorService.updateBatchById(updateCommandList);

        if (!reportStatus.equals(ReportStatusConstant.REPORTED)) {
            return;
        }

        groupMap.forEach((commandId, dataList) ->
            insLogCountCacheService.increaseUploadCount(
                InstructionType.INS_FILTER,
                commandId + "",
                dataList.size()
            )
        );
    }
}
